/*
 * Copyright (C) RT, 2017 by kiterunner_t
 * TO THE HAPPY FEW
 */

package com.zgys.mes;

import com.zgys.mes.common.config.DocmConfig;
import com.zgys.mes.common.config.StartInterceptor;

import com.zgys.mes.util.Log;
import com.zgys.mes.util.deadlock.DeadLockDetector;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


@Component
public class MesStarter implements CommandLineRunner, DisposableBean {
    @Autowired private StartInterceptor startInterceptor;
    @Autowired private DocmConfig docmConfig;
//    @Autowired private TimerScheduler timerScheduler;
    /* todo ： 控制启动顺序 */
    /* 内部服务初始化 */
//    @Autowired private ProcessService processService;

    /* 启动， 可选 读取record_id
     * 检查哪些需要启动定时器，进行周期性的通知HMI进行文档阅读
     * */

    
    public void run(String... args) {
        try {
            Log.init(docmConfig.getLogTrace());
            if (docmConfig.isSwitchStatus()) {
                Log.info("deadlock start");
                DeadLockDetector deadLockDetector = new DeadLockDetector(docmConfig.getCyclingTime(), TimeUnit.SECONDS);
                deadLockDetector.start();
            }

//            JobFactory.init(timerScheduler);
//            timerScheduler.start();
            /* 设置启动成功标记 */
            startInterceptor.setInit();

        } catch (Exception ex) {
            Log.error("mes service start failed。。", ex);
            destroy();
            System.exit(1);
        }
    }

    public void destroy() {
        try {
//            timerScheduler.stop();
            Log.info("mes service stop! ");
        } catch (Exception ex) {
            Log.error("mes service stop failed。", ex);
        }
    }
}

